package com.example.demo.topic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.topic.entity.TFansRecord;
import com.example.demo.topic.service.TFansRecordService;
import com.example.demo.topic.vo.TFansRecordVo;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 粉丝记录表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/tFansRecord")
@Tag(name = "tFansRecord", description = "粉丝记录相关")
public class TFansRecordController {
    @Autowired
    private TFansRecordService tFansRecordService;

    /**
     * 获取粉丝模块列表
     */
    @GetMapping("/getFansList")
    @Operation(summary = "获取粉丝列表接口", description = "获取粉丝列表接口")
    public Result<?> getFansList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                 @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                 TFansRecord fans){
        Page<TFansRecord> page = new Page<>(current,size);
        QueryWrapper<TFansRecord> queryWrapper = new QueryWrapper<>(fans);
        return Result.ok(tFansRecordService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询粉丝记录信息
     */
    @GetMapping("/getFansById")
    @Operation(summary = "根据id查询粉丝记录明细", description = "根据id查询粉丝记录明细")
    public Result<?> getFansById(Integer id){
        return Result.ok(tFansRecordService.getById(id));
    }

    /**
     * 修改粉丝记录信息
     */
    @PutMapping("/updateFans")
    @Operation(summary = "根据id修改粉丝记录明细", description = "根据id修改粉丝记录明细")
    @Transactional
    public Result<?> updateFans(@RequestBody TFansRecord fans){
        return Result.ok(tFansRecordService.updateById(fans));
    }

    /**
     * 新增粉丝记录信息
     */
    @PostMapping("/addFans")
    @Operation(summary = "新增粉丝记录", description = "新增粉丝记录")
    public Result<?> addFans(@RequestBody TFansRecord fans){
        return Result.ok(tFansRecordService.save(fans));
    }

    /**
     * 删除粉丝记录信息
     */
    @DeleteMapping("/deleteFansById")
    @Operation(summary = "删除粉丝记录记录", description = "删除粉丝记录记录")
    @Transactional
    public Result<?> deleteFansById(Integer id){
        return Result.ok(tFansRecordService.removeById(id));
    }

    /**
     * 获取移动端首页关注内容
     */
    @GetMapping("/getFansByUserId")
    @Operation(summary = "获取移动端首页关注内容", description = "获取移动端首页关注内容")
    public Result<?> getFansByUserId(Integer userId,
                                     @RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                     @RequestParam(value = "size", required = false, defaultValue = "5")Integer size){
        Page<TFansRecordVo> page = new Page<>(current,size);
        return tFansRecordService.getFansByUserId(page,userId);
    }

    /**
     * 获取关注人员数量
     */
    @GetMapping("/getConcernCount")
    @Operation(summary = "获取关注人员数量", description = "获取关注人员数量")
    public Result<?> getConcernCount(Integer userId){
        return tFansRecordService.getConcernCount(userId);
    }

    /**
     * 获取关注人员服务列表
     */
    @GetMapping("/getConcernList")
    @Operation(summary = "获取关注人员服务列表", description = "获取关注人员服务列表")
    public Result<?> getConcernList(Integer userId,
                                    @RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                    @RequestParam(value = "size", required = false, defaultValue = "5")Integer size){
        Page<TFansRecordVo> page = new Page<>(current,size);
        return tFansRecordService.getConcernList(page,userId);
    }

    /**
     * 取消关注
     */
    @DeleteMapping("/cancelFans")
    @Operation(summary = "取消关注", description = "取消关注")
    public Result<?> cancelFans(@RequestBody TFansRecord fans){
        QueryWrapper<TFansRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("master_id",fans.getMasterId()).eq("fan_id",fans.getFanId());
        return Result.ok(tFansRecordService.remove(queryWrapper));
    }
}

